package weifei.web.controller;


import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import weifei.web.service.DBService;
import weifei.web.Result;

import java.util.Map;


@RestController
@RequestMapping("/db")
public class DBController
{

    @Data
    public static class DBParam
    {
        @NotBlank @NotNull
        private String tableName;
        private Integer id;
        @NotNull
        private Map<String, Object> map;
        DBParam() {}
    }


    @Autowired
    private DBService dbService;


    @PostMapping("/insert")
    public Result insert(@Valid @RequestBody DBParam param)
    {
        var row = this.dbService.insert(param.getTableName(), param.getMap());
        if (row == 0)
            return Result.failed("插入失败");
        return Result.success("插入成功");
    }

    @PostMapping("/updateByID")
    public Result updateByID(@Valid @RequestBody DBParam param)
    {
        var id = param.getId();
        if (id == null)
            return Result.exception("参数错误");
        var row = this.dbService.updateByID(param.getTableName(), id, param.getMap());
        if (row == 0)
            return Result.failed("更新失败");
        return Result.success("更新成功");
    }

    @PostMapping("/deleteByID")
    @DeleteMapping("/deleteByID")
    public Result updateByID(@RequestParam String tableName, @RequestParam int id)
    {
        var row = this.dbService.deleteByID(tableName, id);
        if (row == 0)
            return Result.failed("删除失败");
        return Result.success("删除成功");
    }


    @GetMapping("/getTableTotalCount")
    @PostMapping("/getTableTotalCount")
    public Result getTableTotalCount(@RequestParam String tableName)
    {
        var count = this.dbService.selectCount(tableName);
        return Result.success("获取成功", count);
    }


    @GetMapping("/getAllTableNameList")
    @PostMapping("/getAllTableNameList")
    public Result getAllTableNameList()
    {
        var list = this.dbService.selectAllTableNameList();
        return Result.success("获取成功", list);
    }


    @GetMapping("/getAllTableColumnNameList")
    @PostMapping("/getAllTableColumnNameList")
    public Result getAllTableColumnNameList(@RequestParam String tableName)
    {
        var list = this.dbService.selectAllTableColumnNameList(tableName);
        return Result.success("获取成功", list);
    }


    /**
     * {
     *     "CONSTRAINT_NAME": "PRIMARY",
     *     "REFERENCED_TABLE_NAME": null,
     *     "REFERENCED_COLUMN_NAME": null
     * }
     * */
    @GetMapping("/getColumnKeyUsage")
    @PostMapping("/getColumnKeyUsage")
    public Result getColumnKeyUsage(
            @RequestParam String tableName,
            @RequestParam String columnName
        )
    {
        var map = this.dbService.selectColumnKeyUsage(tableName, columnName);
        if (map == null)
            return Result.failed("获取失败");
        return Result.success("获取成功", map);
    }


    @GetMapping("/getColumnDefaultValue")
    @PostMapping("/getColumnDefaultValue")
    public Result getColumnDefaultValue(
            @RequestParam String tableName,
            @RequestParam String columnName
        )
    {
        var value = this.dbService.selectColumnDefaultValue(tableName, columnName);
        return Result.success("获取成功", value);
    }


    @GetMapping("/getColumnIsNullable")
    @PostMapping("/getColumnIsNullable")
    public Result getColumnIsNullable(
            @RequestParam String tableName,
            @RequestParam String columnName
        )
    {
        var is = this.dbService.selectColumnIsNullable(tableName, columnName);
        return Result.success("获取成功", is);
    }


    @GetMapping("/getColumnDataType")
    @PostMapping("/getColumnDataType")
    public Result getColumnDataType(
            @RequestParam String tableName,
            @RequestParam String  columnName
        )
    {
        var t = this.dbService.selectColumnDataType(tableName, columnName);
        if  (t == null)
            return Result.failed("获取失败");
        return Result.success("获取成功", t);
    }


    @GetMapping("/getPage")
    @PostMapping("/getPage")
    public Result getPage(
            @RequestParam String tableName,
            @RequestParam int pageIndex
        )
    {
        System.out.println(tableName);
        var list = this.dbService.selectPage(tableName, pageIndex);
        System.out.println(list);
        return Result.success("获取成功", list);
    }


    @GetMapping("/getPageCount")
    @PostMapping("/getPageCount")
    public Result getPageCount(@RequestParam String tableName)
    {
        var count = this.dbService.getPageCount(tableName);
        return Result.success("获取成功", count);
    }


    @GetMapping("/setPageSize")
    @PostMapping("/setPageSize")
    public Result getPageSize(@RequestParam int size)
    {
        if (!this.dbService.setPageSize(size))
            return Result.failed("修改失败");
        return Result.success("修改成功");
    }


    @GetMapping("/getPageSize")
    @PostMapping("/getPageSize")
    public Result getPageSize()
    {
        return Result.success("获取成功", this.dbService.getPageSize());
    }


    @GetMapping("/searchByColumnValue")
    @PostMapping("/searchByColumnValue")
    public Result searchByColumnValue(
        @RequestParam String tableName,
        @RequestParam String columnName,
        @RequestParam String value
        )
    {
        var result = this.dbService.searchByColumnValue(tableName, columnName, value);
        return Result.success("搜索成功", result);
    }
}


